package online.chazi.bootplus.commons.utils;

import org.springframework.lang.Nullable;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

/**
 * 说明：网卡小工具
 * Created by 叉子同学 on 2020-04-13 14:49
 */
public class IfUtils {

    /**
     * 获取本机所有网卡ip地址
     *
     * @return List 返回ip数组
     */
    @Nullable
    public static List<String> getHostIps() {
        try {
            List<String> ips = new ArrayList<>();
            Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
            while (allNetInterfaces.hasMoreElements()) {
                NetworkInterface netInterface = allNetInterfaces.nextElement();
                Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    InetAddress ip = addresses.nextElement();
                    if (ip instanceof Inet4Address
                            && !ip.isLoopbackAddress() //loopback地址即本机地址，IPv4的loopback范围是127.0.0.0 ~ 127.255.255.255
                            && !ip.getHostAddress().contains(":")) {
                        ips.add(ip.getHostAddress());
                    }
                }
            }
            return ips;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 是否内网ip，不支持ipv6
     *
     * @param ip ip地址
     * @return boolean 是否内网ip
     */
    public static boolean isInternalIp(String ip) {
        if (ip.contains(".")) {
            int v = Integer.parseInt(ip.substring(0, ip.indexOf(".", 4)).replace(".", ""));
            return v == 192168  //192.168.*.* 段
                    || v >= 17216 && v <= 17231     //172.16.*.* ~ 172.31.** 段
                    || v >= 100 && v <= 10255;      //10.0.*.* ~ 10.255.*.* 段
        }
        //return "0:0:0:0:0:0:0:1".equals(ip);
        return false;
    }

    public static String getLocalHost() {
        try {
            return Inet4Address.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static List<String> getLocalHosts() {
        return getLocalHosts(true);
    }

    public static List<String> getLocalHosts(boolean internal) {
        List<String> list = new ArrayList<>();
        try {
            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
            while (interfaces.hasMoreElements()) {
                NetworkInterface ni = interfaces.nextElement();
                Enumeration<InetAddress> addresss = ni.getInetAddresses();
                while (addresss.hasMoreElements()) {
                    InetAddress nextElement = addresss.nextElement();
                    String ip = nextElement.getHostAddress();
                    if (!internal || isInternalIp(ip)) {
                        list.add(ip);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

}
